// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init(
  {
    env: cloud.DYNAMIC_CURRENT_ENV
  }
)
const db = cloud.database()
const MAX_LIMIT = 100

// 云函数入口函数
exports.main = async (event, context) => {
   
  
  const num  = event.num;
  const num2 = event.num2;
  // return{
  //   num:num,
  //   num2:num2
  // }
   // 先取出集合记录总数
   const _ = db.command
   const countResult = await db.collection('student').where({
    student_status:_.eq(num).or(_.eq(num2))
  }).count()
   const total = countResult.total
   if(total==0){
     return []
   }else {
    // 计算需分几次取
   const batchTimes = Math.ceil(total / 100)
   // 承载所有读操作的 promise 的数组
   const tasks = []
   
   for (let i = 0; i < batchTimes; i++) {
     const promise = db.collection('student').where({
      student_status: _.eq(num).or(_.eq(num2))
    }).skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
     tasks.push(promise)
   }
   // 等待所有
  //  
   return (await Promise.all(tasks)).reduce((acc, cur) => {
     return {
       data: acc.data.concat(cur.data),
       errMsg: acc.errMsg,
     }
   })
   }
  //  return{
  //    total:total
  //  }
   
}